{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e978cbf",
   "metadata": {},
   "source": [
    "## 1. APP 币本位合约交易-基础订单函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e89a22f4",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|set_order|下单（API原始接口）|\n",
    "|get_order|查询订单（API原始接口）|\n",
    "|get_orders_pending|查看当前挂单|\n",
    "|get_orders_pending_open|查看当前开仓挂单|\n",
    "|get_orders_pending_close|查看当前平仓挂单|\n",
    "|wait_order_FILLED|等待订单成交|\n",
    "|cancel_order|撤销订单（API原始接口）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d8e77fd",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2a69bfc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceCM\n",
    "from binance_interface.app.utils import eprint\n",
    "import paux.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db0a3c75",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceCM = BinanceCM(\n",
    "    key=key,\n",
    "    secret=secret,\n",
    "    proxy_host=proxy_host,\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "trade = binanceCM.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aee41f4",
   "metadata": {},
   "source": [
    "## 3. 下单（API原始接口）  set_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "585d51ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 7108734736,\n",
      "          'symbol': 'DOTUSD_PERP',\n",
      "          'pair': 'DOTUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',\n",
      "          'price': '6.100',\n",
      "          'avgPrice': '0.000',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706193909383},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='DOTUSD_PERP',\n",
    "    side='BUY',\n",
    "    type='LIMIT',\n",
    "    price='6.1',\n",
    "    quantity=1,\n",
    "    positionSide='LONG',\n",
    "    timeInForce='GTC',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e809fab",
   "metadata": {},
   "source": [
    "## 4. 查询订单（API原始接口） get_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "262b76c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 7108734736,\n",
      "          'symbol': 'DOTUSD_PERP',\n",
      "          'pair': 'DOTUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',\n",
      "          'price': '6.100',\n",
      "          'avgPrice': '0.000',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'time': 1706193909383,\n",
      "          'updateTime': 1706193909383},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orderId = set_order_result['data']['orderId']\n",
    "order_result = trade.get_order(\n",
    "    symbol='DOTUSD_PERP',\n",
    "    orderId=orderId,\n",
    ")\n",
    "eprint(order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c92645e",
   "metadata": {},
   "source": [
    "## 5. 查看当前挂单 get_orders_pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab898574",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 7108734736,\n",
      "           'symbol': 'DOTUSD_PERP',\n",
      "           'pair': 'DOTUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',\n",
      "           'price': '6.100',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193909383,\n",
      "           'updateTime': 1706193909383},\n",
      "          {'orderId': 70547715976,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193177536,\n",
      "           'updateTime': 1706193177536},\n",
      "          {'orderId': 70548864413,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',\n",
      "           'price': '2100',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193876902,\n",
      "           'updateTime': 1706193876902},\n",
      "          {'orderId': 70547694568,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',\n",
      "           'price': '2000',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193160778,\n",
      "           'updateTime': 1706193160778},\n",
      "          {'orderId': 70548673979,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193739701,\n",
      "           'updateTime': 1706193739701}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 查询symbol 从start到end全部未成交订单\n",
    "orders_pending_result = trade.get_orders_pending(\n",
    "    symbol='',  # 默认为空，表示全部产品\n",
    "    start='2024-01-01 10:00:00',  # 默认为空，表示不限定订单的起始时间\n",
    "    end='2024-12-26',  # 默认为空，表示不限定订单的终止时间\n",
    ")\n",
    "eprint(orders_pending_result, length=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "451b19c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70547715976,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193177536,\n",
      "           'updateTime': 1706193177536},\n",
      "          {'orderId': 70548673979,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193739701,\n",
      "           'updateTime': 1706193739701},\n",
      "          {'orderId': 70547694568,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',\n",
      "           'price': '2000',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193160778,\n",
      "           'updateTime': 1706193160778},\n",
      "          {'orderId': 70548864413,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',\n",
      "           'price': '2100',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193876902,\n",
      "           'updateTime': 1706193876902}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 查询symbol 从start到end全部未成交订单\n",
    "orders_pending_result = trade.get_orders_pending(\n",
    "    symbol='ETHUSD_PERP',  # 默认为空，表示全部产品\n",
    "    start='2024-01-01 10:00:00',  # 默认为空，表示不限定订单的起始时间\n",
    "    end='2024-12-26',  # 默认为空，表示不限定订单的终止时间\n",
    ")\n",
    "eprint(orders_pending_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf2cdf00",
   "metadata": {},
   "source": [
    "## 6. 查看当前开仓挂单 get_orders_pending_open"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d7c1ef55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70547715976,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193177536,\n",
      "           'updateTime': 1706193177536},\n",
      "          {'orderId': 70547694568,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_Qf3K1XXNtOayzl3M05Ek',\n",
      "           'price': '2000',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193160778,\n",
      "           'updateTime': 1706193160778}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数positionSide默认值为''，表示全部多单和空单\n",
    "orders_pending_open_result = trade.get_orders_pending_open(\n",
    "    symbol='ETHUSD_PERP',\n",
    ")\n",
    "eprint(orders_pending_open_result, length=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f28dcdbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70547715976,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_xAkWbFbtGfP3W6nRPI3J',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': False,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193177536,\n",
      "           'updateTime': 1706193177536}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# positionSide='SHORT' 限制结果为空单\n",
    "orders_pending_open_result = trade.get_orders_pending_open(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(orders_pending_open_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e940d2",
   "metadata": {},
   "source": [
    "## 7. 查看当前平仓挂单 get_orders_pending_close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a852b29d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70548673979,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_NsHOBwSq1znyGgs0TDBz',\n",
      "           'price': '2400',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'SELL',\n",
      "           'positionSide': 'LONG',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193739701,\n",
      "           'updateTime': 1706193739701},\n",
      "          {'orderId': 70548864413,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',\n",
      "           'price': '2100',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193876902,\n",
      "           'updateTime': 1706193876902}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数positionSide默认值为''，表示全部多单和空单\n",
    "orders_pending_close_result = trade.get_orders_pending_close(\n",
    "    symbol='ETHUSD_PERP',\n",
    ")\n",
    "eprint(orders_pending_close_result, length=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ef747a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70548864413,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'ios_H29bLqhYVMPyLC2k9tVt',\n",
      "           'price': '2100',\n",
      "           'avgPrice': '0',\n",
      "           'origQty': '1',\n",
      "           'executedQty': '0',\n",
      "           'cumBase': '0',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'reduceOnly': True,\n",
      "           'closePosition': False,\n",
      "           'side': 'BUY',\n",
      "           'positionSide': 'SHORT',\n",
      "           'stopPrice': '0',\n",
      "           'workingType': 'CONTRACT_PRICE',\n",
      "           'priceProtect': False,\n",
      "           'origType': 'LIMIT',\n",
      "           'time': 1706193876902,\n",
      "           'updateTime': 1706193876902}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# positionSide='SHORT' 限制结果为空单\n",
    "orders_pending_close_result = trade.get_orders_pending_close(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(orders_pending_close_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1c3a3ac",
   "metadata": {},
   "source": [
    "## 8. 等待订单成交 wait_order_FILLED"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "788a2185",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 7108734736,\n",
      "          'symbol': 'DOTUSD_PERP',\n",
      "          'pair': 'DOTUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',\n",
      "          'price': '6.100',\n",
      "          'avgPrice': '0.000',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'time': 1706193909383,\n",
      "          'updateTime': 1706193909383},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orderId = set_order_result['data']['orderId']\n",
    "symbol = set_order_result['data']['symbol']\n",
    "# 堵塞，等待订单完全成交，如果超时后仍未成交，返回订单数据\n",
    "wait_order_filled_result = trade.wait_order_FILLED(\n",
    "    symbol=symbol,\n",
    "    orderId=orderId,\n",
    "    timeout=5,\n",
    ")\n",
    "eprint(wait_order_filled_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c21d0cb7",
   "metadata": {},
   "source": [
    "## 9. 撤销订单（API原始接口） cancel_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f69f561c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 7108734736,\n",
      "          'symbol': 'DOTUSD_PERP',\n",
      "          'pair': 'DOTUSD',\n",
      "          'status': 'CANCELED',\n",
      "          'clientOrderId': 'pPO9llcY29tIyoWJT3fYnS',\n",
      "          'price': '6.100',\n",
      "          'avgPrice': '0.000',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706193919012},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orderId = set_order_result['data']['orderId']\n",
    "symbol = set_order_result['data']['symbol']\n",
    "\n",
    "cancel_order_result = trade.cancel_order(\n",
    "    symbol=symbol,\n",
    "    orderId=orderId,\n",
    ")\n",
    "eprint(cancel_order_result, length=30)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
